#!/bin/bash
RUN_DIR=`pwd`
BASE_DIR=~/gem5
O3FILE=${BASE_DIR}/src/cpu/o3/O3CPU.py
SEARCH1='fetchWidth = Param.Unsigned('
SEARCH2='decodeWidth = Param.Unsigned('
SEARCH3='renameWidth = Param.Unsigned('
SEARCH4='issueWidth = Param.Unsigned('
SEARCH5='commitWidth = Param.Unsigned('

GEM5=${BASE_DIR}/build/ALPHA/gem5.opt
SEPY=${BASE_DIR}/configs/example/se.py
CPUTYPE=detailed
NUMBENCHMARKS=`seq 0 4`

#Benchmark Commands
BENCHMARKS=(
./Benchmarks/qsort/qsort_small
./Benchmarks/MatrixMultiply/MatrixMultiply
./Benchmarks/basicmath/basicmath_small
./Benchmarks/FFT/fft
./Benchmarks/dijkstra/dijkstra_small
)

#Benchmark Commands
BENCHARGS=(
"./Benchmarks/qsort/input_small.dat"
""
""
"4 4096"
"./Benchmarks/dijkstra/input.dat"
)

#Name Prefixes for output (Must have same number as benchmarks
NAMEPREFIX=(
QS
MM
BM
FT
DJ
)

#Test Setups
FW=(1 4 4 4 4 4 8 8 8 8 8)
DW=(1 1 4 4 4 4 4 8 8 8 8)
RW=(1 1 1 4 4 4 4 4 8 8 8)
IW=(1 1 1 1 4 4 4 4 4 8 8)
CW=(1 1 1 1 1 4 4 4 4 4 8)

for testNum in `seq 0 10`;
do

	echo "Building for case ${testNum}" 
	sed -e "s/\(${SEARCH1}[0-9]\)/${SEARCH1}${FW[testNum]}/;
		s/\(${SEARCH2}[0-9]\)/${SEARCH2}${DW[testNum]}/;
		s/\(${SEARCH3}[0-9]\)/${SEARCH3}${RW[testNum]}/;
		s/\(${SEARCH4}[0-9]\)/${SEARCH4}${IW[testNum]}/;
		s/\(${SEARCH5}[0-9]\)/${SEARCH5}${CW[testNum]}/" O3CPU.py > ${O3FILE}
	
	cp ${O3FILE} ${RUN_DIR}/O3${testNum}.txt
	cd ${BASE_DIR}
	scons ./build/ALPHA/gem5.opt -j4		#Use 4 processes

	cd ${RUN_DIR}
	for benchRun in ${NUMBENCHMARKS};
	do
		echo "Running benchmark ${NAMEPREFIX[benchRun]}${testNum}"
		
		${GEM5} \
		--stats-file=${NAMEPREFIX[benchRun]}${testNum}STAT.txt \
		--stdout-file=${NAMEPREFIX[benchRun]}${testNum}OUT.txt \
		--stderr-file=${NAMEPREFIX[benchRun]}${testNum}ERR.txt \
		-re ${SEPY} --cpu-type=${CPUTYPE} --caches --l2cache -c ${BENCHMARKS[benchRun]} -o "${BENCHARGS[benchRun]}"
	done

done

cp orgO3CPU.py ${O3FILE} 

